package com.sk.sourcecircle.activity.home.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.dialog.CancelOrderFragment;
import com.sk.sourcecircle.dialog.TextTipDialogFragment;
import com.sk.sourcecircle.entity.OrderDetailsBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/1/3.
 */

public class OrderDetailsActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tv_action)
    TextView tvAction;
    @BindView(R.id.event_bg)
    ImageView eventBg;
    @BindView(R.id.event_name)
    TextView eventName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.event_ll)
    RelativeLayout eventLl;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.pay_price)
    TextView payPrice;
    private OrderDetailsBean.DataBean dataBean;
    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_order_details;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        dataBean = getIntent().getParcelableExtra("data");
        Glide.with(mContext).load(dataBean.getImg_url()).into(eventBg);
        tvNum.setText(dataBean.getCount() + "");
        tvPrice.setText("￥" + dataBean.getUnit_price());
        eventName.setText(dataBean.getActivity_name());
        tvLocation.setText(dataBean.getCity() + " " + dataBean.getDistrict());
        tvTime.setText(dataBean.getStart_time() + "开始");
        payPrice.setText("￥" + dataBean.getOrder_price());
    }

    @OnClick({R.id.back, R.id.event_ll, R.id.tv_action})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.event_ll:
                break;
            case R.id.tv_action:

                if (MyUtils.isTui(dataBean.getStart_time())) {
                    CancelOrderFragment cancelOrderFragment = new CancelOrderFragment();
                    cancelOrderFragment.setMyCallBack(OrderDetailsActivity.this);
                    cancelOrderFragment.show(getFragmentManager(), "cancel");
                } else {
                    TextTipDialogFragment textTipDialogFragment = new TextTipDialogFragment();
                    textTipDialogFragment.setContent("十分抱歉，距离活动开始仅剩不到三天，无法为您取消订单哦\n若有疑问请联系客服！");
                    textTipDialogFragment.show(getSupportFragmentManager(),"cancel");
                }


                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
            case 1:
                TextTipDialogFragment textTipDialogFragment = new TextTipDialogFragment();
                textTipDialogFragment.setContent("退款申请提交成功,请注意查收!");
                textTipDialogFragment.setMyCallBack(OrderDetailsActivity.this);
                textTipDialogFragment.show(getSupportFragmentManager(), "tip");
                break;
            case 2:
                TextTipDialogFragment textTipDialogFragment1 = new TextTipDialogFragment();
                textTipDialogFragment1.setContent("订单取消成功！");
                textTipDialogFragment1.setMyCallBack(OrderDetailsActivity.this);
                textTipDialogFragment1.show(getSupportFragmentManager(), "tip");
                break;
        }


    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void sure() {
        super.sure();
        finish();
    }

    @Override
    public void cancelOder(String string) {
        super.cancelOder(string);
        loadingDialog.show();
        if (dataBean.getPay_type() == 0) {
            AppApi.wxRefund(dataBean.getOrder_id(), OrderDetailsActivity.this, 0, OrderDetailsActivity.this);

        } else if (dataBean.getPay_type() == 1) {
            AppApi.alRefund(dataBean.getOrder_id(), OrderDetailsActivity.this, 1, OrderDetailsActivity.this);
        } else {
            AppApi.cancelOrder(MyUtils.getCode(mContext), dataBean.getOrder_id(), OrderDetailsActivity.this, 2, OrderDetailsActivity.this);
        }
    }
}
